package ViewOption;


import Pojo.User;
import Service.UserService;
import Utils.BaseMessage;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class userVo {
    private final Logger logger = Logger.getLogger(userVo.class);
    private final UserService userService = new UserService();

    /**
     * 前端传递手机号密码获取User对象存进session
     *
     * @param request 请求对象
     * @return 返回提示对象
     */
    public Object getUserByTelAndPassword(HttpServletRequest request) {
        String tel = request.getParameter("tel");
        String password = request.getParameter("password");
        User user = null;
        try {
            user = userService.getUserByTelephoneAndPassword(tel, password);
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "非法请求，清理联系管理员查看", null);
        }
        logger.info("user=>" + user);
        if (user == null) {
            return new BaseMessage(1, "账号密码错误", null);
        }
        HttpSession session = request.getSession();
        session.setAttribute("userMsg", user);
        return new BaseMessage(0, null, "登录成功");
    }

    /**
     * 注册用户
     *
     * @param tel
     * @param password
     * @param nickname
     * @param email
     * @param username
     * @return
     */
    public Object insertUserByRegister(String tel, String password, String nickname, String email, String username) {
        try {
            if (userService.getUserCountByTelOrEmail(tel, null) > 0) {
                return new BaseMessage(1, "您的手机号被注册过咯", null);
            }
            if (userService.getUserCountByTelOrEmail(null, email) > 0) {
                return new BaseMessage(1, "您的邮箱已经被注册过咯请重新输入", null);
            }
            if (userService.insertUser(tel, password, nickname, email, username) < 1) {
                return new BaseMessage(2, "操作有误，请联系管理员处理问题", null);
            }
        } catch (SQLException throwables) {
            return new BaseMessage(3, "异常问题，请联系管理员", null);
        }
        return new BaseMessage(0, null, "注册成功");
    }

    /**
     * 修改密码
     *
     * @return
     */
    public Object updatePasswordByUserId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("userMsg");
        int userId = user.getUserId();
        String userPassword = user.getUserPassword();

        String oldPassword = request.getParameter("oldPassword");
        String newPassword = request.getParameter("newPassword");
        if (!oldPassword.equals(userPassword)) {
            return new BaseMessage(1, "原密码输入错误", null);
        }
        try {
            if (userService.updatePasswordByUserId(userId, newPassword) < 1) {
                return new BaseMessage(2, "修改失败，具体问题联系管理员", null);
            }
        } catch (SQLException throwables) {
            return new BaseMessage(3, "出现异常问题请联系管理", null);
        }
        return new BaseMessage(0, null, "修改成功");
    }

    /**
     * 修改个人信息
     *
     * @param request
     * @return
     */
    public Object updateUserMsgByUserId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String nickname = request.getParameter("nickname");
        String email = request.getParameter("email");
        String username = request.getParameter("username");
        User user = (User) session.getAttribute("userMsg");
        int userId = user.getUserId();
        String userEmail = user.getUserEmail();
        try {
            if (!email.equals(userEmail)) {
                if (userService.getUserCountByTelOrEmail(null, email) > 0) {
                    return new BaseMessage(1, "您修改的邮箱已存在，请重新输入", null);
                }
            }
            if (userService.updateUserMsgByUserId(nickname, email, username, userId) < 1) {
                return new BaseMessage(2, "修改失败，请联系管理员处理", null);
            }
        } catch (SQLException throwables) {
            return new BaseMessage(3, "出现异常请联系管理员处理", null);
        }
        return new BaseMessage(0, null, "修改成功");
    }

    /**
     * 获取用户信息
     *
     * @param request
     * @return
     */
    public Object getUsersIsAdminBySelective(HttpServletRequest request) {
        int searchCon = Integer.parseInt(request.getParameter("searchCon"));
        String search = request.getParameter("search");
        int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        Map<String, Object> data = new HashMap<>();
        try {
            data.put("data", userService.getUsersIsAdminBySelective(searchCon, search, (pageIndex - 1) * 10));
            data.put("itemsCount", userService.getUsersCountIsAdminBySelective(searchCon, search));
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "出现异常", null);
        }
        return new BaseMessage(0, null, data);
    }

    public Object deleteUserIsAdminByUserId(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        try {
            if (userService.deleteUserIsAdminByUserId(userId) < 1) {
                return new BaseMessage(2, "禁止删除管理员", null);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "出现异常", null);
        }
        return new BaseMessage(0, null, "删除成功");
    }
}
